package com.wqr.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wqr.pojo.Login;
import com.wqr.pojo.User;
import com.wqr.service.UserService;

@Controller
public class UserController {

	@Autowired
    public UserService userService;
	
	@RequestMapping("/manageUser")
	public String user(Model model,User user) throws Exception {
		List<User> list = userService.list(user);
		model.addAttribute("itemList", list);
	    return "manageUser"; 
	}
	@RequestMapping("/userSearchUser")
	public String userSearchUser(@RequestParam("loginId") Integer loginId,Model model,User user) throws Exception {
		user.setLoginId(loginId);
		List<User> list = userService.list(user);
		model.addAttribute("itemList", list);
	    return "userSearchUser"; 
	}
	@RequestMapping("/addUser")
	@ResponseBody// 返回的数据不是html标签的页面，而是其他某种格式的数据时（如json、xml等）使用；
	public int addUser(@RequestBody //读取Request请求的body部分数据,再把HttpMessageConverter返回的对象数据绑定到 controller中方法的参数上
			User user,HttpSession session,Login login) throws Exception {
		//IsLogin(session);
		login = (Login)session.getAttribute("login");
		user.setLoginId(login.getLoginId());
		int a = userService.insertUser(user);
		return a;
	}
	//@RequestMapping(value="/deleteUser/{userStateId}",method=RequestMethod.GET)
	@RequestMapping("/deleteUser")
	@ResponseBody// 返回的数据不是html标签的页面，而是其他某种格式的数据时（如json、xml等）使用；
	public int deleteUser(@RequestBody //读取Request请求的body部分数据,再把HttpMessageConverter返回的对象数据绑定到 controller中方法的参数上
			User user) throws Exception {
		//@pathVariable //将 URL 中占位符参数绑定到控制器处理方法的入参中 URL 中的 {xxx} 占位符可以通过@PathVariable("xxx") 绑定到操作方法的入参中。
		int a = userService.delectUser(user.getUserId());
		return a;
	}
	@RequestMapping("/updateUser")
	@ResponseBody
	public User updateUser(@RequestBody User user) throws Exception {
		User itemList = userService.selectByPrimaryKey(user.getUserId());
		return itemList;
	}
	@RequestMapping("/saveUser")
	@ResponseBody// 返回的数据不是html标签的页面，而是其他某种格式的数据时（如json、xml等）使用；
	public int saveUser(@RequestBody User user,Login login,HttpSession session) throws Exception {
		login = (Login)session.getAttribute("login");
		user.setLoginId(login.getLoginId());
		int a = userService.updateUser(user);
		return a;
	}
	@RequestMapping("/searchUser")
	@ResponseBody
	public List<User> searchUser(@RequestBody User user) throws Exception {
		if(user.getUserName() == ""){
			user.setUserName(null);
		}
		if(user.getGender() == ""){
			user.setGender(null);
		}
		List<User> itemList = userService.selectUserByDIY(user);
	    return itemList;
	}
	@RequestMapping("/searchAllUser")
	@ResponseBody
	public List<User> searchAllUser(User user) throws Exception {
		List<User> itemList = userService.list(user);
	    return itemList;
	}
	@RequestMapping("/userUpdateUser")
	public String userUpdateUser(@RequestParam("loginId") Integer loginId,Model model,User user) throws Exception {
		user.setLoginId(loginId);
		List<User> list = userService.list(user);
		model.addAttribute("itemList", list);
	    return "userUpdateUser"; 
	}
}
